package com.zzuli.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzuli.dao.AreaInfoDao;
import com.zzuli.entity.AreaInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AreaInfoService {

    @Autowired
    private AreaInfoDao areaInfoDao;

    public Map<String, Object> selectAll(int pageNum, int pageSize, AreaInfoEntity areaInfoEntity) {
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
        List<AreaInfoEntity> list = areaInfoDao.selectAll(areaInfoEntity);
        //创建PageInfo对象，并将当前页面数据传入，计算分页相关数据
        PageInfo<AreaInfoEntity> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        //本页数据
        map.put("list", list);
        //总数据条目
        map.put("total", total);
        return map;
    }

    public boolean addArea(AreaInfoEntity areaInfoEntity) {
        return areaInfoDao.insertSelective(areaInfoEntity);
    }

    public boolean updateArea(AreaInfoEntity areaInfoEntity) {
        return areaInfoDao.updateByPrimaryKeySelective(areaInfoEntity);
    }

    public boolean deleteArea(String[] allAreaId){
        for(String areaId : allAreaId){
            areaInfoDao.deleteByPrimaryKey(Integer.parseInt(areaId));
        }
        return true;
    }

    public List<Integer> selectBuildCount() {
        return areaInfoDao.selectBuildCount();
    }
}
